#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using System.Linq;
using Neatly.UI;
using NeatlyEditor.UI;
using UnityEditor;
using UnityEngine;
using UnityEditor.AnimatedValues;

namespace NeatlyEditor
{
    [CustomEditor(typeof(NImageBase), true)]
    [CanEditMultipleObjects]
    public class NImageBaseEditor : NGraphicEditor
    {
        protected SerializedProperty m_UseSpritePacker;
        protected SerializedProperty m_UseBlur;
        protected SerializedProperty m_SpriteName;
        protected SerializedProperty m_Sprite;
        protected GUIContent m_UseSpritePackerContent;
        protected GUIContent m_UseBlurContent;
        protected GUIContent m_SpriteNameContent;
        protected GUIContent m_SpriteContent;
        protected override void OnEnable()
        {
            base.OnEnable();
            m_UseBlurContent = new GUIContent("UseBlur");
            m_UseSpritePackerContent = new GUIContent("UseSpritePacker");
            m_SpriteNameContent = new GUIContent("Sprite Name");
            m_SpriteContent = new GUIContent("Source Image");
            m_UseBlur = serializedObject.FindProperty("m_UseBlur");
            m_UseSpritePacker = serializedObject.FindProperty("m_UseSpritePacker");
            m_SpriteName = serializedObject.FindProperty("m_SpriteName");
            m_Sprite = serializedObject.FindProperty("m_Sprite");
        }
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            SpriteGUI();
            AppearanceControlsGUI();
            RaycastControlsGUI();
            UseSpritePackerGUI();
            NImageBase image = target as NImageBase;
            if (image.UseSpritePacker)
            {
                EditorGUILayout.HelpBox("当前非TP模式，请记得修改.", MessageType.Info);
            }
            serializedObject.ApplyModifiedProperties();
        }
        protected virtual void SpriteGUI()
        {
            NImageBase image = target as NImageBase;
            EditorGUILayout.PropertyField(m_UseBlur, m_UseBlurContent);
            EditorGUILayout.PropertyField(m_UseSpritePacker, m_UseSpritePackerContent);
            if (!image.UseSpritePacker)
            {
                EditorGUILayout.PropertyField(m_SpriteName, m_SpriteNameContent);
            }
            EditorGUILayout.PropertyField(m_Sprite, m_SpriteContent);
        }
        protected void UseSpritePackerGUI()
        {
            NImageBase image = target as NImageBase;
            if (m_UseSpritePacker.boolValue)
            {
                image.SetMaterial(null);
            }
            else
            {
                if (m_UseSpritePacker.boolValue != image.UseSpritePacker)
                {
                    ResetBySprite();
                }
                else
                {
                    if (m_Sprite.objectReferenceValue != image.sprite)
                    {
                        ResetBySprite();
                    }
                    if (!string.IsNullOrEmpty(image.SpriteName) && !image.SpriteName.Equals(m_SpriteName.stringValue))
                    {
                        ResetSprite(m_SpriteName.stringValue, m_UseBlur.boolValue);
                    }
                }
            }
        }
        private void ResetBySprite()
        {
            NImageBase image = target as NImageBase;
            image.SpriteName = m_Sprite.objectReferenceValue ? m_Sprite.objectReferenceValue.name : "";
            m_SpriteName.stringValue = image.SpriteName;
            ResetSprite(image.SpriteName, image.UseBlur);
        }
        private void ResetSprite(string spriteName, bool useBlur)
        {
            Material mat;
            Sprite sprite = UIUtility.FindImageSprite(spriteName, useBlur, out mat);
            m_Material.objectReferenceValue = mat;
            m_Sprite.objectReferenceValue = sprite;
        }
    }
}